<?php
class Place_WidgetController extends Clu_Controller_Action_Widget
{
	public function init()
	{
		//require_once (APP_PATH.'/modules/place/Model/Place.php');
		//require_once (APP_PATH.'/modules/article/Model/Article.php');
		//require_once (APP_PATH.'/modules/event/Model/Event.php');
		$this->mPlace = new place_Model_Place();
		$this->mArticle = new article_Model_Service();
		parent::init();
	}
	public function simpleSearchAction()
    {
    	$sess = new Zend_Session_Namespace(CLU_NS);
    	$this->viewParams['form'] = new place_Form_PlaceSearch();
    	$actionUrl = $this->widgetService->getView()->hvnUrl(array('module' => 'place', 'action'=>'search'), 'nocontroller');
        $this->viewParams['form']->cluBuild(array('action' => 'search', 'action_url' => $actionUrl, 'cluSearchMode' => 'simpleSearch'));
        if($this->issetParam('city_id'))
        {
        	$this->viewParams['cityId'] = $this->getParam('city_id');
        	$data = array('city_id' => $this->getParam('city_id'));
			$this->viewParams['form']->populateCity($data);
        }
        if($this->issetParam('raw_name'))
        {
        	$this->viewParams['raw_name'] = $this->getParam('raw_name');
        	$data = array('raw_name' => $this->getParam('raw_name'));
			$this->viewParams['form']->populateCity($data);
        }
        
        if($this->issetParam('rtype'))
        	$this->viewParams['rtype'] = $this->getParam('rtype');
    }
    public function listArticleAction()
    {
    	if ($this->issetParam('place_id'))
    		$this->viewParams['place_id'] = $this->getParam('place_id');
    	if($this->issetParam('listArticle'))
    	{
			$listArticle = $this->getParam('listArticle');
			sort($listArticle);
	    	$this->viewParams['listArticle'] = $listArticle;
    	}
    }
    
    public function galleryAction()
    {
    	if ($this->issetParam('place_id'))
    		$placeId = $this->getParam('place_id');
    	if ($this->issetParam('gallery_id'))
    		$galleryId = $this->getParam('gallery_id');
		if (!isset($galleryId))
		{
			$this->viewParams['id'] = '';
			return;		
		}
		//require_once(APP_PATH . "/modules/node/Model/NodeService.php");
		$nService = new node_Model_NodeService();
		$nodeTypeInfo = $nService->getNodeTypeInfo("gallery");
		$ret = $nService->getNodeData($nodeTypeInfo, $galleryId);
		if ($ret['success'])
		{
			$gallery = $ret['node'];
			$this->viewParams['images'] = unserialize($gallery['images']);
			$this->viewParams['id'] = $galleryId;
			$this->viewParams['placeId'] = $placeId;
		}
		else
    	{
			$this->viewParams['id'] = '';
			return;		
		}				     	
    }
    /* related places when viewing a place */
 	public function relatedPlacesAction() 
    {
    	if ($this->issetParam('rtype'))
			$rtype = $this->getParam('rtype');
		else
			$rtype = null;
		if ($this->issetParam('place_id'))
			$placeId = $this->getParam('place_id');
		else
			$placeId = null;
		if ($this->issetParam('city_id'))
			$cityId  =   $this->getParam('city_id');
		else
			$cityId = null;
		//$districtId  =   $this->getParam('district_id');
		$data = array("rtype"=>$rtype,"city_id"=>$cityId /*,"district_id"=>$districtId */);
		$listPlace = $this->mPlace->search($data);     
		$paginator = $this->generatePaginator($listPlace, 1,11,11, 'Jumping');
		$listArticle = array();
		$this->viewParams['placeId'] = $placeId;
		$this->viewParams['pList']    =   $paginator->getCurrentItems();
		foreach($this->viewParams['pList']   as  $key=>$value ){        
			if($value['id']      !==   $placeId){
			$listArticle[$value['id']]  =   $this->mArticle->getArticleData($value['article_id']);
			}   
			$listArticle[$placeId] = null;
		}
		if(!empty($listArticle)){
			$this->viewParams['listArticle']   = $listArticle;
		}else{
			$this->viewParams['listArticle'] = array() ;
		}
	}
		
	public function generatePaginator($select, $pageNumber, $itemsPerPage, $pageRange, $style)
    {
		$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
		$paginator->setItemCountPerPage($itemsPerPage);
		$paginator->setCurrentPageNumber($pageNumber);
		$paginator->setPageRange($pageRange);
		Zend_Paginator::setDefaultScrollingStyle($style);
		return $paginator;
    }
    
    public function featuredGalleryAction()
    {
    	//require_once (APP_PATH . "/modules/admin/Model/Weight.php");
    	$mWeight = new admin_Model_Weight();
    	//now get all the content_id from this $widgetPositionId;
    	$data['widget_position_id'] = $this->widget['id'] ;
    	$currentItems = $mWeight->searchCurrentItemsByWidgetPositionId($data);
    	$places = array();
		foreach ($currentItems as $item)
		{
			$places[] = $this->mPlace->getData($item['content_id'], array('get_article' => 1, 'get_vicinities' => 0));
		}
		$this->viewParams['places'] = $places;
		//die_a($places[0]);
    }
    
    public function editTabMenuAction()
    {
//     	$this->viewParams['id'] = $this->getParam('id');
//    	$this->viewParams['currentTab'] = $this->getParam('currentTab');
		if(isset($_REQUEST['id']))
     		$this->viewParams['id'] = $_REQUEST['id'];
     	else
     		$this->viewParams['id'] = $_REQUEST['place_id'];
     	//$placeName = $this->mPlace->getData($this->viewParams['id'], array('get_article' => 1, 'get_vicinities' => 0));
     	//$this->viewParams['placeName'] = $placeName['article']['article_title'];
     	$this->viewParams['placeName'] = "TODO";
//    	$this->viewParams['currentTab'] = "thumbnail";
    }
    
    public function topCategoryInCityAction()
    {
    	if($this->issetParam(('city_id')))
    		$cityId = $this->getParam('city_id');
    	if($this->issetParam('category_id'))
    	{
	    	$categoryId = $this->getParam('category_id');
	    	$this->viewParams['category_id'] = $categoryId;
    	}
    }
    
}
?>